package com.runjian.rundo.iot.agent.common.network.tcp.parser;

import com.runjian.rundo.iot.agent.common.network.tcp.config.TcpNetworkProperties;

import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * 基于长度字段 解码器
 *
 * @author CaoQingHua
 */
public class LengthBasedPayloadParser implements PayloadParser {

    private final TcpNetworkProperties valueObject;

    public LengthBasedPayloadParser(TcpNetworkProperties valueObject) {
        this.valueObject = valueObject;
    }

    @Override
    public ByteToMessageDecoder getParser() {
        int maxFrameLength = valueObject.getLengthField().getMaxFrameLength();
        int lengthFieldOffset = valueObject.getLengthField().getLengthFieldOffset();
        int lengthFieldLength = valueObject.getLengthField().getLengthFieldLength();
        int lengthAdjustment = valueObject.getLengthField().getLengthAdjustment();
        int initialBytesToStrip = valueObject.getLengthField().getInitialBytesToStrip();
        return new LengthFieldBasedFrameDecoder(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment,
            initialBytesToStrip);
    }
}
